package com.example.message.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.message.entity.Message;

/**
 * 通知Mapper接口
 */
@Mapper
public interface MessageMapper {
    
    /**
     * 根据接收人ID查询通知列表
     * @param page 
     * @param receiverId 接收人ID
     * @param messageType 通知类型（可选）
     * @param readStatus 阅读状态（可选）
     * @return 通知列表
     */
    @Select({"<script>",
        "SELECT n.*, nr.receive_id, nr.read_status, nr.handle_status",
        "FROM t_message n",
        "INNER JOIN t_message_receive nr ON n.message_id = nr.message_id",
        "WHERE nr.receiver_id = #{receiverId}",
        "AND n.del_flag = 0",
        "AND n.status = 1",
        "AND nr.del_flag = 0",
        "<if test='messageType != null'>",
        "AND n.message_type = #{messageType}",
        "</if>",
        "<if test='readStatus != null'>",
        "AND nr.read_status = #{readStatus}",
        "</if>",
        "ORDER BY n.send_time DESC",	
        "</script>"
    })
    List<Message> selectMessageByReceiverId(Page<Message> page, @Param("receiverId") Long receiverId,
                                           @Param("messageType") Integer messageType,
                                           @Param("readStatus") Integer readStatus);
    @Select({"<script>",
        "SELECT n.*, nr.receive_id, nr.read_status, nr.handle_status",
        "FROM t_message n",
        "INNER JOIN t_message_receive nr ON n.message_id = nr.message_id",
        "WHERE nr.receiver_id = #{receiverId}",
        "AND n.del_flag = 0",
        "AND n.status = 1",
        "AND nr.del_flag = 0",
        "<if test='messageType != null'>",
        "AND n.message_type = #{messageType}",
        "</if>",
        "<if test='readStatus != null'>",
        "AND nr.read_status = #{readStatus}",
        "</if>",
        "ORDER BY n.send_time DESC",
        "<if test='size != 0'>",
        "limit #{size}",
        "</if>",
        "</script>"
    })
    List<Message> findListByReceiverId(@Param("receiverId") Long receiverId,
                                           @Param("messageType") Integer messageType,
                                           @Param("readStatus") Integer readStatus, int size);
    
    /**
     * 查询未读通知数量
     * @param receiverId 接收人ID
     * @return 未读通知数量
     */
    @Select({
        "SELECT COUNT(*)",
        "FROM t_message_receive nr",
        "INNER JOIN t_message n ON n.message_id = nr.message_id",
        "WHERE nr.receiver_id = #{receiverId}",
        "AND nr.read_status = 0",
        "AND n.del_flag = 0",
        "AND n.status = 1",
        "AND nr.del_flag = 0"
    })
    Integer selectUnreadCount(@Param("receiverId") Long receiverId);
    
    /**
     * 根据业务类型和业务ID查询通知
     * @param businessType 业务类型
     * @param businessId 业务ID
     * @return 通知列表
     */
    @Select({
        "SELECT *",
        "FROM t_message",
        "WHERE business_type = #{businessType}",
        "AND business_id = #{businessId}",
        "AND del_flag = 0",
        "AND status = 1",
        "ORDER BY send_time DESC"
    })
    List<Message> selectMessageByBusiness(@Param("businessType") String businessType,
                                         @Param("businessId") String businessId);
    
    /**
     * 插入通知记录
     * @param message 通知实体
     * @return 插入结果
     */
    @Insert({
        "INSERT INTO t_message (title, content, message_type, priority, send_method, ",
        "target_type, target_ids, target_names, business_type, business_id, ",
        "expire_time, status, sender_id, sender_name, send_time, ",
        "create_time, del_flag)",
        "VALUES (#{title}, #{content}, #{messageType}, #{priority}, #{sendMethod}, ",
        "#{targetType}, #{targetIds}, #{targetNames}, #{businessType}, #{businessId}, ",
        "#{expireTime}, #{status}, #{senderId}, #{senderName}, #{sendTime}, ",
        "NOW(), 0)"
	})
	@Options(useGeneratedKeys = true, keyProperty = "messageId")
	int insertMessage(Message message);

	@Select({ "<script>", "select message_id, title, content, message_type, priority, send_method, "
			+ "target_type, target_ids, target_names, business_type, business_id, "
			+ "expire_time, status, sender_id, sender_name, send_time, "
			+ "create_time, update_time, del_flag from t_message where del_flag = 0 "
			+ "<if test='m.title != null and m.title != \"\"'> AND u.title like concat('%', #{m.title}, '%') </if>"
			+ "${m.params.dataScope}", "</script>" })
	List<Message> selectList(Page<Message> page,@Param("m")  Message message);

	@Select({ "select message_id, title, content, message_type, priority, send_method, "
			+ "target_type, target_ids, target_names, business_type, business_id, "
			+ "expire_time, status, sender_id, sender_name, send_time, "
			+ "create_time, update_time, del_flag from t_message where del_flag = 0 and message_id =#{id}" })
	Message findById(Long id);

	/**
	 * 更新通知记录
	 * @param message 通知实体
	 * @return 更新结果
	 */
	@Update({
		"<script>",
		"UPDATE t_message SET",
		"<if test='title != null'>title = #{title},</if>",
		"<if test='content != null'>content = #{content},</if>",
		"<if test='messageType != null'>message_type = #{messageType},</if>",
		"<if test='priority != null'>priority = #{priority},</if>",
		"<if test='sendMethod != null'>send_method = #{sendMethod},</if>",
		"<if test='targetType != null'>target_type = #{targetType},</if>",
		"<if test='targetIds != null'>target_ids = #{targetIds},</if>",
		"<if test='targetNames != null'>target_names = #{targetNames},</if>",
		"<if test='businessType != null'>business_type = #{businessType},</if>",
		"<if test='businessId != null'>business_id = #{businessId},</if>",
		"<if test='expireTime != null'>expire_time = #{expireTime},</if>",
		"<if test='status != null'>status = #{status},</if>",
		"<if test='senderId != null'>sender_id = #{senderId},</if>",
		"<if test='senderName != null'>sender_name = #{senderName},</if>",
		"<if test='sendTime != null'>send_time = #{sendTime},</if>",
		"update_time = NOW()",
		"WHERE message_id = #{messageId}",
		"AND del_flag = 0",
		"</script>"
	})
	int updateMessage(Message message);
}